* Figure A2 - Year-by-year effects of insurance coverage, controlling for baseline
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* keeping balanced panel
keep if bal_panel3==1

sort id mkt_year

replace Tvillage = 0 if mkt_year == 2005

rename fraction_hy_k HY
rename fraction_cashcrop_k cashcrop

local outcomevars "totalrevenues_w1_real kval_w1_real fin_revenues_real totalcosts_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 fin_costs_real totalprofit_w1_real profit_k_w1_real HY cashcrop plotsize_ha"

sort id survey_year
by id: gen first_year=survey_year if [_n]==1 

foreach var of local outcomevars {
gen `var'_t0_temp = `var' if survey_year==first_year
bys id: egen `var'_t0 =min(`var'_t0_temp)   
drop `var'_t0_temp
}

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

replace flyer_hyv=1 if flyer_hyv_exp==1

** Don't include flyer_exp in marketing assignment

local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

lab var cost_k_w1_real "Total agricultural costs (INR)"
lab var profit_k_w1_real "Total agricultural profits (INR)"
lab var kval_w1_real "Total agricultural revenues (INR)"
lab var fin_costs_real "Total insurance costs (INR)"
lab var fin_revenues_real "Total insurance revenues (INR)"
lab var totalcosts_w1_real "Total costs (INR)"
lab var totalrevenues_w1_real "Total revenues (INR)"
lab var totalprofit_w1_real "Total profit (INR)"
lab var HY "Fraction of HYV crops"
lab var cashcrop "Fraction of cash-crops"
lab var plotsize_ha "Area cultivated (in hectares)"


// Year-by-year effects IV - Ind 
foreach ovar of varlist cost_k_w1_real kval_w1_real profit_k_w1_real fin_costs_real fin_revenues_real totalcosts_w1_real totalrevenues_w1_real totalprofit_w1_real HY cashcrop plotsize_ha {
gen `ovar'effect = .
gen `ovar'se = .
gen `ovar'se2 = .
gen `ovar'se_2 = .
forvalues i = 2006/2013 {
ivreg `ovar' (s_policy_units=treat_year `uniqinst') firstt_2007 lost_w3 resurveyed `ovar'_t0 if mkt_year == `i' , r cluster(villageno)
replace `ovar'effect = _b[s_policy_units] if mkt_year == `i'
replace `ovar'se = _se[s_policy_units] if mkt_year == `i'
lab var `ovar'effect "No. of Policies Purhchased - Effect"
lab var `ovar'se "Standard Errors"
replace `ovar'se2  = `ovar'effect+2*`ovar'se if mkt_year == `i'
replace `ovar'se_2 = `ovar'effect-2*`ovar'se if mkt_year == `i'
}

local l`ovar': variable label `ovar'
twoway (rarea `ovar'se2 `ovar'se_2 mkt_year, sort astyle(ci ci))(scatter `ovar'effect mkt_year , sort msymbol(O) c(l)), title("`l`ovar''", size(s)) ylabel(,angle (30)) xlabel(2005(2)2013) xtitle("Year") graphregion(color(white)) plotregion(icolor(white)) legend(order (2 1) label(2 "Estimated coefficient") label(1 "95% confidence interval"))
graph save iv`ovar', replace 
}

grc1leg ivcost_k_w1_real.gph ivkval_w1_real.gph ivprofit_k_w1_real.gph ivfin_costs_real.gph ivfin_revenues_real.gph ivtotalcosts_w1_real.gph ivtotalrevenues_w1_real.gph ivtotalprofit_w1_real.gph ivHY.gph ivcashcrop.gph ivplotsize_ha.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../output/graphs/af2.emf", replace




